package br.com.toscano.gobra.view.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.arquillian.core.api.annotation.Inject;

import br.com.toscano.automacao.constant.ConstantesMensagem;
import br.com.toscano.gobra.business.ConfiguracaoBC;
import br.com.toscano.gobra.business.LicencaBC;

@WebFilter("*.jsf")
public class ConfiguracaoFilter implements Filter {

	@Inject
	ConfiguracaoBC configuracaoBC;
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletResponse res = (HttpServletResponse) response;
		HttpServletRequest req = (HttpServletRequest) request;

		if (req.getSession().getAttribute("logoPrefeitura") == null && ConfiguracaoBC.getConfiguacao() != null) {
			req.getSession().setAttribute("nomeArquivo", ConfiguracaoBC.getConfiguacao().getNomeImagem());
			req.getSession().setAttribute("logoPrefeitura", ConfiguracaoBC.getConfiguacao().getImagem());
			req.getSession().setAttribute("contentType", ConfiguracaoBC.getConfiguacao().getUrlImagemMunicipio());
		}

		//	login		
		if (req.getSession().getAttribute(ConstantesMensagem.CONTEXTO_USUARIO_SESSAO) == null && 
				!req.getRequestURI().startsWith(req.getContextPath() + "/javax.faces.resource") &&
				!req.getRequestURI().startsWith(req.getContextPath() + "/ApresentarFiguraServlet.jsf") &&
				!req.getRequestURI().endsWith("login.jsf") &&
				!req.getRequestURI().endsWith(req.getContextPath() + "/") &&
				!req.getRequestURI().endsWith(req.getContextPath())) {

			System.out.println("REDIRECT LOGIN: " + req.getRequestURI());
			
            res.sendRedirect("./login.jsf");
            
		} else {

			// licenca de uso
			if ((!"".equals(LicencaBC.getCodigoValidadeLicenca())) && 
					!req.getRequestURI().endsWith("licenca_edit.jsf") &&
					!req.getRequestURI().startsWith(req.getContextPath() + "/javax.faces.resource") &&
					!req.getRequestURI().startsWith(req.getContextPath() + "/ApresentarFiguraServlet.jsf") &&
					!req.getRequestURI().endsWith("login.jsf") &&
					!req.getRequestURI().endsWith(req.getContextPath() + "/") &&
					!req.getRequestURI().endsWith(req.getContextPath())) {

				System.out.println("REDIRECT LICENCA: " + req.getRequestURI());
				
	            res.sendRedirect("./licenca_edit.jsf");
			} else {

				// configuracao
				if (ConfiguracaoBC.getConfiguacao() == null && 
						!req.getRequestURI().endsWith("configuracao_edit.jsf") &&
						!req.getRequestURI().endsWith("configuracao_list.jsf") &&
						!req.getRequestURI().startsWith(req.getContextPath() + "/javax.faces.resource") &&
						!req.getRequestURI().startsWith(req.getContextPath() + "/ApresentarFiguraServlet.jsf") &&
						!req.getRequestURI().endsWith("login.jsf") &&
						!req.getRequestURI().endsWith(req.getContextPath() + "/") &&
						!req.getRequestURI().endsWith(req.getContextPath())) {
					
					System.out.println("REDIRECT cONFIGURACAO: " + req.getRequestURI());
		            res.sendRedirect("./configuracao_edit.jsf");
				} else {
					
					chain.doFilter(request, response);
				}
			}
		}
	}

	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
